package com.xiaolin.sports.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaolin.sports.common.RspResult;
import com.xiaolin.sports.common.enums.PayType;
import com.xiaolin.sports.common.enums.Status;
import com.xiaolin.sports.model.ImgGroup;
import com.xiaolin.sports.model.ParentInfo;
import com.xiaolin.sports.model.ProductOrder;
import com.xiaolin.sports.page.Page;
import com.xiaolin.sports.pay.wepay.common.WechatBridge;
import com.xiaolin.sports.reqVo.CourseSignVO;
import com.xiaolin.sports.service.ICourseService;
import com.xiaolin.sports.service.IProductOrderService;
import com.xiaolin.sports.utils.AmountArithUtil;
import com.xiaolin.sports.utils.OrderEncryptUtils;
import com.xiaolin.sports.utils.PayUtils;
import com.xiaolin.sports.view.CourseBuyerView;
import com.xiaolin.sports.view.CourseListView;
import com.xiaolin.sports.vo.CourseQueryVO;
import com.xiaolin.sports.wechat.common.Constants;
import com.xiaolin.sports.wechat.model.WechatUserInfo;

@RequestMapping("course")
@Controller
public class CourseController extends BaseController {
	
	private Logger logger = LoggerFactory.getLogger(CourseController.class);
	@Autowired
	private ICourseService courseService;
	@Autowired
	private IProductOrderService productOrderService;
	
	@RequestMapping(value={"","/","list"})
	public String courseList(Model model) {
		ImgGroup group = new ImgGroup();
		group.setPosition("1");
		group.setShelves(1);
		model.addAttribute("topBannerList",contentManageService.selectListByParam(group));
		return "course/list";
	}
	
	@RequestMapping("list/query")
	public @ResponseBody Object courseListQuery(CourseQueryVO vo) {
		Page<CourseListView> page = new Page<CourseListView>();
		page.setPageNo(vo.getPage());
		page.setPageSize(vo.getRows());
		WechatUserInfo userInfo = getWechatUserInfo();
		vo.setOpenid(userInfo.getOpenid());
		page.setParams(vo);
		List<CourseListView> list = courseService.selectListForWapByPage(page);
		page.setResults(list);
		return page;
	}
	
	@RequestMapping("detail/{courseId}")
	public String detail(Model model, @PathVariable Integer courseId) throws UnsupportedEncodingException {
		WechatUserInfo userInfo = getWechatUserInfo();
		CourseListView courseInfo = courseService.selectForWapByParams(userInfo.getOpenid(), courseId);
		model.addAttribute("course", courseInfo);
		if(courseInfo.getCourseDetails() != null) {
			model.addAttribute("desc", new String(courseInfo.getCourseDetails(),"UTF-8"));
		}
		return "course/detail";
	}
	
	/**
	 * 课程购买人信息
	 * @auther 胡启萌
	 * @Date 2017年4月14日
	 * @param vo
	 * @return
	 */
	@RequestMapping("json/buyer")
	public @ResponseBody Object courseBuyer(CourseQueryVO vo) {
		try {
			Page<CourseBuyerView> page = new Page<CourseBuyerView>();
			page.setPageNo(vo.getPage());
			page.setPageSize(3);
			page.setParams(vo);
			List<CourseBuyerView> list = null;//courseService.selectCourseBuyerByPage(page);
			page.setResults(list);
			return page;
		} catch (Exception e) {
			logger.error("--courseBuyer, error={}", e);
		}
		return null;
	}

	/**
	 * 课程报名详情页
	 * @auther 胡启萌
	 * @Date 2017年4月14日
	 * @param model
	 * @param courseId
	 * @return
	 */
	@RequestMapping("sign/detail/{courseId}")
	public String sign(Model model, @PathVariable Integer courseId) {
		WechatUserInfo userInfo = getWechatUserInfo();
		model.addAttribute(Constants.WECHAT_USERINFO, userInfo);
		CourseListView courseInfo = courseService.selectForWapByParams(userInfo.getOpenid(), courseId);
		ParentInfo parent = parentInfoService.selectByOpenid(userInfo.getOpenid());
		model.addAttribute("course", courseInfo);
		model.addAttribute("parent", parent);
		return "course/signDetail";
	}
	
	/**
	 * 单人报名订单创建
	 * @auther 胡启萌
	 * @Date 2017年4月14日
	 * @param vo
	 * @return
	 */
	@RequestMapping("sign/order/create")
	@ResponseBody Object signOrderCreate(CourseSignVO vo) {
		try {
			WechatUserInfo userInfo = getWechatUserInfo();
			vo.setOpenid(userInfo.getOpenid());
			vo.setHeadImg(userInfo.getHeadimgurl());
			vo.setCity(userInfo.getCountry() + " " + userInfo.getCity());
			vo.setNickName(userInfo.getNickname());
			vo.setChannel(PayType.WEIXIN.getCode());
			ProductOrder view = productOrderService.addUPdateOrder(vo);
			Map<String, Object> map = new HashMap<>();
			map.put("orderNo", view.getOrderNo());
			map.put("amount", view.getTotalAmount().toString());
			return new RspResult(Status.SUCCESS, OrderEncryptUtils.getSignUrl(map));
		} catch (Exception e) {
			logger.error("--signOrderCreate, error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
}
